#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>


//const char* exe = "otherExec";

//多进程版
int main()
{
    pid_t id = fork();

    if(id == 0)
    {
        //char *const myargv[] = {
        //    "ls","-l","-a",NULL 
        //};
        // 子进程执行进程替换
        printf("before: i am a process,pid: %d, ppid: %d\n",getpid(),getppid());
        //sleep(5);
        // 要执行的程序 、 怎样执行该程序 、 ...为可变参数列表
        //execl("/usr/bin/ls","ls","-a","-l",NULL);//这类方法的标准写法
        //execl("/usr/bin/top","top",NULL);
        //execv("usr/bin/ls",myargv);
        //execl("./otherExec","otherExec",NULL);
        execl("usr/bin/bash","bash","test.sh",NULL);
        printf("after: i am a process,pid: %d, ppid: %d\n",getpid(),getppid());
        exit(0);//防止子进程继续运行后续代码
    }

    //父进程
    pid_t ret = waitpid(id,NULL,0);
    if(ret > 0)
    {
        printf("等待子进程返回成功,father pid: %d, ret id: %d\n",getpid(),ret);
    }
    sleep(5);

    return 0;
}
